package com.blb.mapper;

import com.blb.pojo.Department;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface DepartmentMapper {

    /**
     * 子查询配置关联查询
     * @return
     */
    @Results(id = "deptMap",value = {
            @Result(id = true,property = "id",column = "id"),
            //配置员工集合
            @Result(property = "employees",column = "id",
            many = @Many(select = "com.blb.mapper.EmployeeMapper.selectEmployeesByDeptId"))
    })
    @Select("select * from t_department")
    List<Department> selectAll();

    /**
     * 连接查询配置关联查询
     * @param id
     * @return
     */
    @Results(id = "deptMap2",value = {
            @Result(id = true,property = "id",column = "d_id"),
            @Result(property = "name",column = "d_name"),
            @Result(property = "address",column = "address"),
            @Result(property = "employees",
            many = @Many(resultMap = "com.blb.mapper.EmployeeMapper.employeeMap"))
    })
    @Select("select e.id e_id,e.name e_name,e.age,e.gender,e.position,e.image,e.dept_id,\n" +
            "  d.id d_id,d.name d_name,d.address\n" +
            "  from t_employee e join t_department d\n" +
            "  on e.dept_id = d.id" +
            "  where d.id = #{id}")
    Department selectById(Long id);

    void insertDepartment(Department department);

    void updateDepartment(Department department);

    void deleteDepartment(Long id);
}
